package com.aipanda.entity;

import java.math.BigDecimal;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.aipanda.pojo.Setting;
import com.aipanda.util.SettingUtils;

/**
 * 购物详单
 * 
 * @param quantity 数量
 * @param product 产品
 * @param cart 购物车
 * @author wanyuxiang
 * 
 */
@Entity
@Table(name = "aipanda_cart_item")
public class CartItem extends BaseEntity {
	private static final long serialVersionUID = 2979296789363163144L;
	public static final Integer MAX_QUANTITY = 10000;
	private Integer quantity;
	private Product product;
	private Cart cart;

	@Column(nullable = false)
	public Integer getQuantity() {
		return this.quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="product",nullable = false, updatable = false)
	public Product getProduct() {
		return this.product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="cart",nullable = false)
	public Cart getCart() {
		return this.cart;
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	@Transient
	public long getPoint() {
		if ((getProduct() != null) && (getProduct().getPoint() != null)&& (getQuantity() != null)){
			return getProduct().getPoint()* getQuantity();
		}
		return 0L;
	}

	@Transient
	public int getWeight() {
		if ((getProduct() != null) && (getProduct().getWeight() != null)&& (getQuantity() != null)){
			return getProduct().getWeight()* getQuantity();
		}
		return 0;
	}

	/**
	 * 获得对应会员等级的产品单价
	 * @return
	 */
	@Transient
	public BigDecimal getUnitPrice() {
		if ((getProduct() != null) && (getProduct().getPrice() != null)) {
			Setting setting = SettingUtils.get();
			if ((getCart() != null) && (getCart().getMember() != null)&& (getCart().getMember().getMemberRank() != null)) {
				MemberRank memberRank = getCart().getMember().getMemberRank();//获得当前会员对应的等级
				Map<MemberRank, BigDecimal> memberRankAll = getProduct().getMemberPrice();//获得会员等级对应的价格Map
				//如果产品关联的会员等级包含了当前会员的等级
				if ((memberRankAll != null) && (!memberRankAll.isEmpty())&& (memberRankAll.containsKey(memberRank))){
					return setting.setPriceScale(memberRankAll.get(memberRank));
				}
				//否则采用会员等级优惠比例
				if (memberRank.getScale() != null){
					return setting.setPriceScale(getProduct().getPrice().multiply(new BigDecimal(memberRank.getScale())));
				}
			}
			//再否则直接等于当前产品的价格
			return setting.setPriceScale(getProduct().getPrice());
		}
		return new BigDecimal(0);
	}

	/**
	 * 产品合计金额
	 * @return  每种产品的总金额
	 */
	@Transient
	public BigDecimal getSubtotal() {
		if (getQuantity() != null){
			return getUnitPrice().multiply(new BigDecimal(getQuantity()));//单价*数量
		}
		return new BigDecimal(0);
	}

	/**
	 * 判断库存是否不足
	 * 
	 * @return
	 */
	@Transient
	public boolean getIsLowStock() {
		return (getQuantity() != null)&& (getProduct() != null)&& (getProduct().getStock() != null)
				&& (getQuantity() > getProduct().getAvailableStock());
	}

	@Transient
	public void add(int quantity) {
		if (quantity > 0)
			if (getQuantity() != null){
				setQuantity(getQuantity() + quantity);
			}else{
				setQuantity(quantity);
			}
	}
}
